﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data;
using Data.Realm;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.IO;
using System.Drawing;

namespace Client
{
    static public class Tractor
    {
        static private bool started = false;
        static private KeyValuePair<int, int> position;
        static private String name;
        static private MainForm mainForm;
        static private LoginForm loginForm;
        static private AStarForm gridForm;
        static private AStarNode2D goalNode;
        static private Map map;
        static private int? height;
        static public Image Image { get; set; }

        static public bool Started 
        {
            get { return Tractor.started; }
            set { Tractor.started = value; }
        }

        static public int? Height
        {
            get { return Tractor.height; }
            set { Tractor.height = value; }
        }

        static public KeyValuePair<int, int> Position
        {
            get { return Tractor.position; }
            set { Tractor.position = value; }
        }

        static public Map Map
        {
            get { return Tractor.map; }
            set { Tractor.map = value; }
        }

        static public LoginForm LoginForm
        {
            get { return Tractor.loginForm; }
            set { Tractor.loginForm = value; }
        }

        static public MainForm MainForm
        {
            get { return Tractor.mainForm; }
            set { Tractor.mainForm = value; }
        }

        static public AStarNode2D GoalNode
        {
            get { return Tractor.goalNode; }
            set { Tractor.goalNode = value; }

        }
        static public AStarForm GridForm
        {
            get { return Tractor.gridForm; }
            set { Tractor.gridForm = value; }
        }

        static public String Name
        {
            set
            {
                Tractor.name = value;
                //mainForm.agentNameLabel.Text = value;
            }
            get { return Tractor.name; }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainForm mainForm = new MainForm();
            Tractor.MainForm = mainForm;
            Application.Run(mainForm);
        }

        internal static void NewGame()
        {
            Tractor.Started = true;
            Tractor.Image = Properties.Resources.GetRandomPawn(1);
            Tractor.Position = new KeyValuePair<int, int>(0, 0);
            string path = "map.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(World));
            StreamReader reader = new StreamReader(path);
            World world = (World)serializer.Deserialize(reader);
            MainForm mainForm = MainForm;
            mainForm.AgentPanel.Visible = true;
            mainForm.DisconnectMenuItem.Visible = true;
            mainForm.ConnectMenuItem.Visible = false;
            Map = new Map(world.fields);
            Map.RenderMap();

            reader.Close();
         }

        internal static void MoveUp()
        {
            if (Position.Value < (Map.Size -1)) {
                Position = new KeyValuePair<int, int>(Position.Key, Position.Value + 1);
                Tractor.Image = Properties.Resources.GetRandomPawn(1);
                Map.RenderMap();
            }
        }

        internal static void MoveLeft()
        {
            if (Position.Key > 0)
            {
                Position = new KeyValuePair<int, int>(Position.Key-1, Position.Value);
                Tractor.Image = Properties.Resources.GetRandomPawn(3);
                Map.RenderMap();
            }
        }

        internal static void MoveRight()
        {
            if (Position.Key < (Map.Size - 1))
            {
                Position = new KeyValuePair<int, int>(Position.Key+1, Position.Value);
                Tractor.Image = Properties.Resources.GetRandomPawn(2);
                Map.RenderMap();
            }
        }

        internal static void MoveDown()
        {
            if (Position.Value > 0)
            {
                Position = new KeyValuePair<int, int>(Position.Key, Position.Value - 1);
                Tractor.Image = Properties.Resources.GetRandomPawn(4);
                Map.RenderMap();
            }
        }

        internal static void GeneticAlgorithm(int tractorFuel)
        {
            GeneticAlgorithm ga = new GeneticAlgorithm(tractorFuel);
            Chromosome ch = ga.RunAlgorithm();
            MessageBox.Show("Zysk: " + ch.Fitness + " zł.");
        }
        
        internal static void Astar()
        {
            Client.AStar astar = new Client.AStar();

           // AStarNode2D GoalNode = new AStarNode2D(null, null, 0, 9, 9);
            if (GoalNode == null)
            {
                GoalNode = new AStarNode2D(null, null, 0, 9, 9);
            }
            AStarNode2D StartNode = new AStarNode2D(null, GoalNode, 0, 0, 0);

            StartNode.GoalNode = GoalNode;
            astar.FindPath(StartNode, GoalNode);

            //Map = new Map(world.fields);

            Map.PrintSolution(astar.Solution);
                //PrintSolution(astar.Solution);
        }
    }
}
